<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<script type="text/javascript">
    cheakInfo();
    var totalCosts ;//实收
    var totalCharges ;//应收

    //触发事件
    function cheakInfo(){
        $('#inpNo').keydown(function(e){
            if(e.keyCode==13){
                var visitNo=$("#inpNo").val();
                $("#list").html("");
                totalCosts=0.00;
                totalCharges =0.00;
                $("#otherCharges").val("");
                if(visitNo!=null && visitNo!=''){
                    $.ajax({
                         type:'post',
                         url:'${ctx}/fiance/inpSettleMaster/getPayments?visitNo='+visitNo,
                         async:false,
                         success:function(data){
                             if(data!=null){
                                 if(data.settledIndicator=='1'){
                                     toastr.warning("该病人已经结算完成");
                                     return false;
                                 } else if(data.dischargeDate==null){
                                     toastr.warning("该病人未办理出院手续");
                                     return false;
                                 }else{
                                     $('#name').val(data.patVisitIndex.name);
                                     $("#hospCount").val(data.zhuyuanTians);
                                     $("#chargeType").val(data.chargeType);
                                     $("#admissionDate").val(data.admissionDate);
                                     $("#deptAdmissionTo").val(data.deptAdmissionTo);
                                     $("#enterDeptDate").val(data.deptStayed);
                                     $("#dischargeDate").val(data.dischargeDate);
                                     $("#prepayment").val(data.patPrepayment.money);
                                     var totalMoney =0.0;
                                     totalMoney = data.patPrepayment.money;
                                     settle(visitNo,totalMoney);
                                 }
                             }
                         }
                    });
                 /*   $("#prepayments").load('${ctx}/finance/inpSettleMaster/getPayments?visitNo='+visitNo,'',function(){
                        settle(visitNo);
                    });*/
                }else{
                    toastr.warning("请输入住院号进行查询");
                }
            }
        });
    }
    //添加支付类型
    function addType(staus,i){
        var leftHtml="";
        leftHtml+= '<div class="form-group" id="addType">'+
        '<label class="col-sm-3 control-label" for="payType">支付方式：</label>'+
        '<div class="col-sm-3">'+
        '<select name="payType" id="payType" class="form-control" data-parsley-required="true">'+
        <c:forEach items="${fns:getDictList('PAY_WAY_DICT')}" var="type">
        '<option value="${type.value}">${type.label}</option>'+
        </c:forEach>
        '</select>'+
        '</div>'+
        '<label class="col-sm-2 control-label" for="payMoney">付款：</label>'+
        '<div class="col-sm-3">'+
        '<input type="text" class="form-control" name="money" id="payMoney" style="width: 60px;">'+
        '</div>'+
        '<i class="fa fa-minus" style="cursor: pointer" title="删除支付类型" onclick="addType(\'2\',this);"></i>'+
        '</div>';
        if(staus=='1'){
            $(i).parent().after(leftHtml);
        }else if(staus=='2'){
            $(i).parent().remove();
        }
    }
    //结算
    function settle(visitNo,totalMoney){
        $.ajax({
            type:'post',
            url:'${ctx}/finance/inpBillDetail/index?visitNo='+visitNo,
            success:function(data){
                var trHtml=true;
                var tableHtml="";
                tableHtml+= "  <thead><tr> " +
                "<th>收据项目</th> " +
                "<th>应收金额</th> " +
                "<th>实收金额</th> " +
                "<th>收据项目</th> " +
                "<th>应收金额</th> " +
                "<th>实收金额</th> " +
                "<th>收据项目</th> " +
                "<th>应收金额</th> " +
                "<th>实收金额</th> " +
                "</tr> </thead><tbody>";
                for(var i=0;i<data.length;i++){
                    if(i%3==0 && !trHtml && i!=0){
                        tableHtml+='</tr>';
                        trHtml=true;
                    }
                    if(trHtml){
                        tableHtml+='<tr>';
                        trHtml=false;
                    }
                    tableHtml+='<td>'+data[i].itemClass+'</td> ' +
                    ' <td>'+data[i].costs+'</td> ' +
                    '<td>'+data[i].charges+'</td>'
                    totalCosts += Number(data[i].costs);
                    totalCharges +=  Number(data[i].charges);
                }

                totalCharges = Math.floor(totalCharges*100)/100;
                if(data.length%3!=0){
                    for(var i=0;i<3-data.length%3;i++){
                        tableHtml+="<td></td><td></td><td></td>";
                    }
                }
                tableHtml+='</tr>';
                tableHtml += '<tr><td><font color="red">合计:</font></td>' +
                '<td><font color="red">'+totalCosts+'</font></td>' +
                '<td><font color="red">实收:</font></td>' +
                '<td><font color="red">'+totalCharges+'</font></td><td></td><td></td><td></td><td></td><td></td></tr></tbody>;';
                $("#list").html(tableHtml);

                var change ="";
                var otherChange = "";
                if(totalMoney>totalCosts){//预交金大于总费用
                    change += (totalMoney-totalCharges).toFixed(4) ;
                }
                if(totalMoney<totalCosts){//预交金小于总费用
                    otherChange += (totalCharges - totalMoney).toFixed(4) ;
                    $("#otherCharges").val(otherChange);
                }
                $("#charges").val(change);
            }
        });
    }
    //确认结算
    function confirmSettle(){
       if(totalCharges==0){
           toastr.warning("没有可以结算的费用");
       }
        var totalMOney = 0.00;//实收
        $("input[name=money]").each(function(){
            var money =$(this).val();
            totalMOney += Number(money);
        });
        var cash = $("#otherCharges").val();//另收
        if(cash>totalMOney){
            toastr.warning("请输入收款金额");
            return ;
        }
        var changes = $("#charges").val();
        var payType = new Array();
        var payMoney=new Array();
        $("select[name=payType]").each(function(){
            var type=$(this).val();
            payType.push(type);

        });
        $("input[name=money]").each(function(){
            var money =$(this).val();
            payMoney.push(money);
        });
        var visitNo=$("#inpNo").val();
        $.ajax({
             type:'post',
             url:'${ctx}/fiance/inpSettleMaster/save?visitNo='+visitNo+'&payType='+payType+'&payMoney='+payMoney+"&changes="+changes,
             success:function(data){
                 if(data.code=='success'){
                     toastr.success("结算成功");
                     if(data.data!=null){
                         printPdf('${ctx}/fiance/inpSettleMaster/settlePrint','rcptNo='+data.data);
                     }
                     loadDiv('mainCenterDiv','${ctx}/finance/patPrepayment/index');
                     return;
                 }else{
                     toastr.error("收费失败");
                     loadDiv('mainCenterDiv','${ctx}/finance/patPrepayment/index');
                     return;
                 }
             }
        });

    }
    //计算找零
  function theCharge() {
      var totalMOney = 0.00;//收款
      $("input[name=money]").each(function(){
          var money =$(this).val();
          totalMOney += Number(money);
      });
        var cash = $("#otherCharges").val();//另收
       if(cash>totalMOney){
          toastr.warning("实收金额小于应收金额，请重新收款");
          return ;
       }
        var change = Number(totalMOney-cash);
        $("#charges").val(change);

    }

    //医保报销
      $('#medicalInsur').on("click",function(){
              var visitNo=$("#inpNo").val();
              if(visitNo!=null && visitNo!=''){
                  $.ajax({//查询是否医保住院登记
                      type: "POST",
                      url: "${ctx}/finance/medicalInsurance/findSickVisitInfo?visitNo="+visitNo,
                      dataType: "json",
                      success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
                          if (data.code == "success") {
                              toastr.success(data.data);
                              $.ajax({//上传医保住院信息
                                  type: "POST",
                                  url: "${ctx}/finance/medicalInsurance/saveKC27?visitNo="+visitNo,
                                  dataType: "json",
                                  success: function(data) {
                                      if (data.code == "success") {
                                          toastr.success(data.data);
                                          return;
                                    }else{
                                          toastr.success(data.data);
                                          return;
                                      }
                                  },error: function (data) {
                                      toastr.success(data.data);
                                      return;
                                  }
                              });
                          }else{
                              toastr.success(data.data);
                              return;
                          }
                      },
                      error: function (data) {
                      }
                  });
              }else{
                  toastr.warning("医保报销前请先输入住院号");
              }
      });
    //医保回传
    $("#medicalReturn").on("click",function(){
        var visitNo=$("#inpNo").val();
        if(visitNo!=null && visitNo!=''){
            $.ajax({
                type: "POST",
                url: "${ctx}/finance/medicalInsurance/findRecordMasterInp?visitNo="+visitNo,
                dataType: "json",
                success: function(data) {
                    if (data.code == "success") {
                         data.data=10.00;
                        $("#medicalInsurCostsAll").val(data.data);
                        addMedicalPay();
//                        toastr.success(data.data)；
                    }else{
                        toastr.success(data.data);
                        return;
                    }
                },
                error: function (data) {
                }
            });
        }else{
            toastr.warning("结算前请先输入住院号");
        }
    });
//医保撤回
    $('#medicalInsurRefund').on("click",function(){
        var visitNo=$("#inpNo").val();
        if(visitNo!=null && visitNo!=''){
            $.ajax({
                type: "POST",
                url: "${ctx}/finance/medicalInsurance/medicalInsurRefund?visitNo="+visitNo,
                dataType: "json",
                success: function(data) {
                    if (data.code == "success") {
                        toastr.success(data.data);
                    }else{
                        toastr.success(data.data);
                        return;
                    }
                },
                error: function (data) {
                }
            });
        }else{
            toastr.warning("医保撤回前请先输入住院号");
        }
    });
    function addMedicalPay(){
        var tongc =$("#medicalInsurCostsAll").val();
        tongc = 10.00;
//        alert(tongc);
        if(tongc!=null && tongc!='' && tongc>0){
            var payMoneyType = $("#payType");
            var payHtml="";
            payHtml+= '<div class="form-group" id="addType">'+
            '<label class="col-sm-3 control-label" for="payType">支付方式：</label>'+
            '<div class="col-sm-3">'+
            '<select name="payType" id="payType" class="form-control" data-parsley-required="true" readonly="true">'+
            <c:forEach items="${fns:getDictList('PAY_WAY_DICT')}" var="type">
            <c:choose>
            <c:when test="${type.label=='统筹支付'}">
            '<option value="${type.value}" selected="selected">${type.label}</option>' +
            </c:when>
            </c:choose>
            </c:forEach>
            '</select>'+
            '</div>'+
            '<label class="col-sm-2 control-label" for="payMoney">付款：</label>'+
            '<div class="col-sm-3">'+
            '<input type="text" class="form-control" name="money" id="payMoney" value='+tongc+' style="width: 60px;">'+
            '</div>'+
            '</div>';

            if(totalCharges>tongc){
                payMoneyType.before(payHtml);
                theCharge();
            }else{
                payMoneyType.html(payHtml);
                theCharge();
            }
        }
    }
</script>
    <div class="content-wrap bg-default">
        <div class="wrapper" style="bottom: 50px;">
            <div class="row">
                <div class="col-lg-12" id="patientInfo">
                    <section class="panel panel-default">
                        <div class="panel-body" >
                            <form id="searchForm" method="post" class="form-horizontal">
                                <div class="form-group">
                                    <label class="col-sm-1 control-label" for="inpNo">住院号：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" id="inpNo" name="visitNo" >
                                    </div>
                                    <label class="col-sm-1 control-label" for="name">姓名：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" name="patVisitIndex.name" id="name" >
                                    </div>
                                    <label class="col-sm-1 control-label" for="hospCount">住院天数：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" id="hospCount" name="hospCount" >
                                    </div>
                                    <label class="col-sm-1 control-label" for="chargeType">费别：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" name="chargeType" id="chargeType" >
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-1 control-label" for="admissionDate">入院时间：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" id="admissionDate" name="admissionDate" >
                                    </div>
                                    <label class="col-sm-1 control-label" for="deptAdmissionTo">入院科室：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" name="deptAdmissionTo" id="deptAdmissionTo" >
                                    </div>
                                    <label class="col-sm-1 control-label" for="enterDeptDate">所在科室：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" name="enterDeptDate" id="enterDeptDate">
                                    </div>
                                    <label class="col-sm-1 control-label" for="dischargeDate">出院时间：</label>
                                    <div class="col-sm-2">
                                        <input type="text" class="form-control" name="dischargeDate" id="dischargeDate">
                                    </div>
                                </div>
                            </form>
                        </div>
                    </section>
                </div>
            </div>
            <div class="row">
                <section class="panel">
                    <div class="panel-body">
                    <div class="col-lg-7" id="billDetails">
                        <table class="table table-bordered mg-t" id="list">

                        </table>
                    </div>
                <div class="col-lg-5" id="payMoneyType">
                    <form  method="post" class="form-horizontal">
                        <div class="form-group" id="payType">
                            <label class="col-sm-3 control-label" for="payWay">支付方式：</label>
                            <div class="col-sm-3">
                                <select name="payType" class="form-control"  id="payWay">
                                    <c:forEach items="${fns:getDictList('PAY_WAY_DICT')}" var="type">
                                        <c:choose>
                                            <c:when test="${type.label=='现金支付'}">
                                                <option value="${type.value}" selected="selected">${type.label}</option>
                                            </c:when>
                                            <c:otherwise>
                                                <option value="${type.value}">${type.label}</option>
                                            </c:otherwise>
                                        </c:choose>

                                    </c:forEach>
                                </select>
                            </div>
                            <label class="col-sm-2 control-label" for="payMoney">付款：</label>
                            <div class="col-sm-3">
                                <input type="text" class="form-control" name="money" id="payMoney" style="width: 60px;">
                            </div>
                            <i class="fa fa-plus" style="cursor: pointer" title="增加支付类型" onclick="addType('1',this);"></i>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label" for="otherCharges">另收：</label>
                            <div class="col-sm-3">
                                <input type="text" class="form-control" id="otherCharges" >
                            </div>
                            <label class="col-sm-2 control-label" for="charges">找零：</label>
                            <div class="col-sm-3">
                                <input type="text" class="form-control" name="payMoney" id="charges"  onclick="theCharge()">
                            </div>
                        </div>
                   </form>
                </div>
                     </div>
                    </section>
            </div>
            <div class="row">
                <section class="panel">
                    <div class="panel-body">
                        <div class="col-lg-4" id="prepayments">
                            <form  method="post" class="form-horizontal">
                                <div class="form-group">
                                    <label class="col-sm-3 control-label" for="prepayment">总预交金：</label>
                                    <div class="col-sm-3">
                                        <input type="text" class="form-control" id="prepayment"  name="amount">
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="col-lg-8">
                    <form  method="post" class="form-horizontal">
                        <%--<div class="form-group">
                            <label class="col-sm-1 control-label" for="otherCharges">另收：</label>
                            <div class="col-sm-2">
                                <input type="text" class="form-control" id="otherCharges" >
                            </div>
                            <label class="col-sm-1 control-label" for="charges">找零：</label>
                            <div class="col-sm-2">
                                <input type="text" class="form-control" name="payMoney" id="charges" >
                            </div>
                        </div>--%>
                       <%-- <div class="form-group">
                            <label class="col-sm-1 control-label" for="cash">现金：</label>
                            <div class="col-sm-2">
                                <input type="text" class="form-control" id="cash" >
                            </div>
                            <label class="col-sm-1 control-label" for="charges"></label>
                            <div class="col-sm-2">
                                <input type="text" class="form-control" name="payMoney" id="theCharges" onclick="theCharge();"readonly="true">
                            </div>
                        </div>--%>
                    </form>
                </div>

                    </div>
                    </section>
            </div>
            <div class="row pull-right">
                <input type="button" class="btn btn-success" value="医保报销" id="medicalInsur" />
                <input type="button" class="btn btn-success" value="医保回传" id="medicalReturn" />
                <input type="button" class="btn btn-success" value="医保撤回" id="medicalInsurRefund" />
                <input type="button" class="btn btn-success" value="确认收费" onclick="confirmSettle();">
                <input type="hidden" id="medicalInsurCostsAll"><%--住院医保报销总额--%>
            </div>
        </div>
    </div>